//	Hi5 gallery fix
//		by Ruben Llibre (ruben.llibre@gmail.com)
//	Creation Date: 7/7/2006
//	Version: 0.1
//----------------------------------------------------------------------------
//	CHANGELOG:
//	0.1: initial version
//      0.2: Almost functional, loads images, some times, displays them raw
//-----------------------------------------------------------------------------
// ==UserScript==
// @name          Hi5 Gallery Fix
// @namespace     http://diveintogreasemonkey.org/download/
// @description   Simple Script to fix HI5 galleries
// @include       http://www.hi5.com/friend/profile/displayGallery.do*
// ==/UserScript==

/* /html/body/div[@id='p_container']/table/tbody/tr/td[@id='p_center']/table[2]/tbody/tr/td/div[1]/table/tbody/tr/td[3]/table/tbody/tr/td[1]/div/img
/html/body/div[@id='p_container']/table/tbody/tr/td[@id='p_center']/table[2]/tbody/tr/td/div[1]/table/tbody/tr/td[1]/div[@id='photodetails'] (cuadro de fotos)
pic=\d{1,2}$ (pa searchandreplace imagen)
*/

//easier getting external pages
function getPage(uri, pages) {
	http = new XMLHttpRequest();
	http.open("GET", uri, false);
	http.send(null);
	return http.responseText;
}
//get xpah query result in specified document
function xpath(query) {
    return document.evaluate(query, doc, null,
        XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
}

function generateAllImgPages(upperBound,currentPicIndex) { 
	var imgPages = new Array(upperBound - 1); 
	var bReplace = document.URL.indexOf("pic=") == -1; 
	for (var i = 0; i < upperBound; i++) { 
		if (bReplace) { 
			imgPages[i] = document.URL.replace("artistType=", "pic=" + i.toString()); } 
		else { 
			imgPages[i] = document.URL.replace("pic=" + currentPicIndex.toString(), "pic=" + i.toString()); } 
	} 
	return imgPages; 
}

function hi5FixGallery()
{

	//find upperbound of pictures ( 1 of [19] )
	var divCenterPhoto = document.getElementById("center_photo");
	//find span that contains words for upper and current bound
	//WARNING: 
	//	ASSUMES FIRST SPAN INSIDE DIV IS RIGHT
	var rawText = divCenterPhoto.getElementsByTagName("span").item(0).textContent;
	//extract current and upperbound from raw text
		// "/" necesary for literal regex's 
		//"/g" specifies global search (aka: not just first one)
	var regExLiteral = /[\d][^s]/g; 
	var bounds = rawText.match(regExLiteral);
	var upperBound = bounds[1];
	var currentPicIndex = bounds[0]-1; 	
	
	//start something
	var pagesArray = generateAllImgPages(upperBound,currentPicIndex);
	
	var ImgTagRegExp = /<img src=\"http:\/\/.*\" border=\"0\">/;
	var URLRegExp = /(http:.*)(?=\" border)/
	
	for ( var i=0 ; i< pagesArray.length ; i++) 
	{
	   var imgTgPage =  getPage(pagesArray[i]);
	   var imgTag = imgTgPage.match(ImgTagRegExp) 
	   var imgURL = imgTag.toString().match(URLRegExp)[0] ;
	   //proceed to insert
	   var ImgPlaceHolder, newElement;
		ImgPlaceHolder = document.getElementById('photodetails');
		if (ImgPlaceHolder) {
			newElement = document.createElement('img');
			newElement.src = imgURL;
			newElement.alt = 'Image #' + i;
			ImgPlaceHolder.parentNode.insertBefore(newElement, ImgPlaceHolder.nextSibling);
		}
	}

}

//add code to the load of the page
window.addEventListener('load', hi5FixGallery(),true);
//function() { document.body.innerHTML = newBody; }
	